package com.eolcum.evo.thread;

import android.content.Intent;

import com.eolcum.evo.persistence.Apartment;
import com.google.gson.Gson;
import android.content.Context;

public class BroadcastManager implements Runnable {

	final String tag = "BroadcastManagerThread";
	final int cycleFrequency = 1000;

	private Context context;
	Apartment apartment;

	// Broadcasts apartment if any change in the apartment object occurs.
	public BroadcastManager() {
	}

	@Override
	public void run() {
		while (true) {
			boolean changed = true;

			if (changed) {
				Gson gson = new Gson();
				Intent intent = new Intent();

				intent.setAction("com.eolcum.evo.EVO_ACTION");
				intent.putExtra("function_name", "getApartment");
				intent.putExtra("apartment", gson.toJson(apartment));
				context.sendBroadcast(intent);
			}
			try {
				Thread.currentThread();
				Thread.sleep(cycleFrequency);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}

	public Apartment getApartment() {
		return apartment;
	}

	public void setApartment(Apartment apartment) {
		this.apartment = apartment;
	}

	public Context getContext() {
		return context;
	}

	public void setContext(Context context) {
		this.context = context;
	}
}
